package com.example.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
public class AllotSwitchController {

	/**
	 * 设置开关
	 * @param request
	 * @param valid
	 * @return
	 */
	@PostMapping("/setAllotSwitch")
	public String setAllotSwitch(HttpServletRequest request,String valid) {
		ServletContext context = request.getServletContext();
		context.setAttribute("autoAllotAskerSwitch", valid);
		return "success";
	}
	

	/**
	 * 查询开关（查询是否是自动分配咨询师）
	 * @param request
	 * @return
	 */
	@PostMapping("/getAllotSwitch")
	public Map<String,Object> getAllotSwitch(HttpServletRequest request){
		ServletContext context = request.getServletContext();
		String isAuto = (String) context.getAttribute("autoAllotAskerSwitch");
		
		//第一次读的时候，给一个默认值on
		if(isAuto == null || isAuto.equals("")) {
			isAuto = "on";
		}
		
		//拼接表格数据
		List<Map<String,Object>> list = new ArrayList<>();
		Map<String,Object> kgmap = new HashMap<>();
		kgmap.put("isAuto", isAuto);
		list.add(kgmap);
		
		//返回结果
		Map<String,Object> map = new HashMap<>();
		map.put("code", "0");
		map.put("msg", "success");
		map.put("count", 1);
		map.put("data", list);
		return map;
	}
}
